
/*
 * @Author: HanYaJun
 * @Date: 2023-01-04 15:39:32
 * @Email: hanyajun@wedobest.com.cn
 * @Description: 方块预制
 */


const trace = function (...args) {
    console.log('ItemBlock【HYJ】', ...args);
}

const traceError = function (...args) {
    console.error('ItemBlock【HYJ】', ...args);
}

const { ccclass, property } = cc._decorator;

@ccclass
export default class ItemBlock extends cc.Component {
    /**执行次数 */
    private executeCount: number = 1;
    /**最后创建的方块索引值 */
    private imgIndex: number = 1;

    /**
     * @description: 当附加到一个激活的节点上或者其节点第一次激活时候调用
     */
    protected onLoad(): void {
        // 执行次数
        let progressLevel = cc.sys.localStorage.getItem('executeCount');
        if (progressLevel) {
            this.executeCount = parseInt(progressLevel);
        } else {
            cc.sys.localStorage.setItem('executeCount', this.executeCount);
        }
    }

    /**
     * @description: 方块数据初始化
     * @param {object} data {position: 位置, index: 颜色值, diffIndex: 不同颜色值设置索引}
     * @return {*}
     */
    public initData(data: { position: cc.Vec2 }): void {
        this.node.setPosition(data.position);
        cc.sys.localStorage.setItem('executeCount', this.executeCount + 1);
    }

    /**
     * @description: 设置方块图片
     */
    public createBlockImg(data: { serialNumber?: number, pastCreation?: number }): void {
        // 图片地址
        let imgStr: string = `texture/card_`;
        // 方块节点
        let newImage: cc.Sprite = cc.find('mask/newImage', this.node).getComponent(cc.Sprite);
        if (data.pastCreation) {
            imgStr = `texture/card_${data.pastCreation}`;
        } else {
            cc.sys.localStorage.setItem('finallyCreate', data.serialNumber);
            imgStr = `texture/card_${data.serialNumber}`;
        }
        cc.loader.loadRes(imgStr, cc.SpriteFrame, (error: Error, resource: any) => {
            if (error) {
                traceError('error:', error);
            } else {
                newImage.spriteFrame = resource;
            }
        });
    }

    /**
     * @description: 点击事件
     * @param {cc} event
     */
    public clickEvent(event: cc.Event.EventTouch): void {
        let name = event.target.name;
        switch (name) {
            case 'colorBlock': {
                // 获取编号
                let serialNumber: cc.Label = this.node.getChildByName('serialNumber').getComponent(cc.Label);
                // 获取最后创建的方块预制索引
                let finallyIndex = cc.sys.localStorage.getItem('finallyCreate');
                let imgIndex: number = null;
                if (finallyIndex) {
                    imgIndex = parseInt(finallyIndex);
                } else {
                    imgIndex = 0;
                }
                trace('方块编号值,erialNumber.string:', serialNumber.string);
                trace('图片索引值,imgIndex:', imgIndex);
                if (serialNumber.string) {
                    if (Number(serialNumber.string) === imgIndex + 1) {
                        // 编号相同
                        trace('编号相同!');
                        this.node.dispatchEvent(new cc.Event.EventCustom('correct', true));
                    } else {
                        // 编号不同
                        trace('编号不同!');
                        this.node.dispatchEvent(new cc.Event.EventCustom('error', true));
                    }
                }
                break;
            }
            default: {
                break;
            }
        }
    }

    /**
     * @description: 当该组件被销毁时调用
     */
    protected onDestroy(): void {
    }
}
